/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-04-21 14:57:46
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 16:17:08
 */

'use strict';
define([
    '../../../framework/base/baseView',
    './filter',
    '../../../framework/bean/productSet',
    '../../../framework/bean/feed',
    'text!../tpl/createSets.html'
], function(
    BaseView,
    FilterView,
    productSetModel,
    feedModel,
    tpl
) {

    function me() {
        BaseView.apply(this, arguments);
        var self = this;
        this.init = function(callback) {
            self.feedId = localStorage.getItem('weFeedId');
            self.callback = callback;
            self.getMeta(function() {
                self.getFeed(function() {
                    self.render();
                });
            });
        }

        this.render = function() {
            self.$root = $('#createSets-modal');
            if (self.$root.length == 0) {
                $('body').append(I18N.parse(tpl, 'sets'));
                self.$root = $('#createSets-modal');
            }
            this.$root.find('.modal-dialog-ndp').css('width', '800px'); //更改大小
            this.$root.removeAttr('tabindex'); //去除,避免select2无法搜索
            this.$root.find('button.btn-ok').removeAttr('data-dismiss');
            this.$root.modal('show');
            this.event();
        }

        this.event = function() {
            $('#add-filter').off('click').on('click', function(e) {
                new FilterView().init(self.$root, self.meta, self.feed);
            });
            $('#add-filter').trigger('click');
            self.$root.find('button.btn-ok').off('click').on('click', $.proxy(self.onOk, self));
            self.$root.on('hidden.bs.modal', function() {
                self.$root.remove();
            });
        }

        this.onOk = function(e) {
            if (self.valicateParams()) {
                return;
            }
            var params = self.getParams();
            if(params===true){
                return;
            }
            console.info(JSON.stringify(params))
            productSetModel.create(params, function(result) {
                if (self.callback) {
                    self.$root.modal('hide');
                    self.callback(result);
                }
            });
        }

        /**
         * 查询后台的 meta 数据,提早缓存出来.
         *
         * @method     getMeta
         */
        this.getMeta = function(initCallback) {
            var params = {
                "feed_id": self.feedId
            }
            productSetModel.getMeta(params, function(result) {
                self.meta = result;
                initCallback();
            });
        }

        /**
         * Get the feed.
         *
         * @method     getFeed
         * @param      {Function}  getFeedCallback  The get feed callback
         */
        this.getFeed = function(getFeedCallback) {
            var params = {
                "feedId": self.feedId
            }
            feedModel.getFeed(params, function(result) {
                self.feed = result;
                getFeedCallback();
            });
        }


        this.valicateParams = function() {
            var name = self.$root.find('input[name=name]').val();
            if (name.trim().length == 0) {
                self.$root.find('input[name=name]').testRemind(I18N.getText('sets', 'notNull'));
                self.$root.find('input[name=name]').focus()
                return true;
            }

            if (self.$root.find('.fb-filter .form-group-filter').length == 0) {
                self.kit.alert2(I18N.getText('sets', 'noFilter'));
                $('#add-filter').trigger('click');
                return true;
            }
            return false;
        }

        this.getParams = function() {
            var params = {
                "feed_id": self.feedId,
                "name": self.$root.find('input[name=name]').val(),
                "rule": { "and": [] }
            }
            var isError = false;
            self.$root.find('.fb-filter .form-group-filter').each(function() {
                var filterArr = $(this).data('filter').getParams();
                if (filterArr === true) {
                    isError = true;
                    return false;
                }
                params.rule.and = params.rule.and.concat(filterArr);
            });
            console.info(JSON.stringify(params))
            if (isError) {
                return isError;
            }
            return params;
        }
    }
    return me;
});
